package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.TrolleyMapper;
import com.sdry.model.hpf.Trolley;
import com.sdry.service.hpf.TrolleyService;

/** 
* 台车业务层实现类
* @author lian 
* @version 1.0
* @time 2018年11月1日 下午3:12:37
*/

@Service
public class TrolleyServiceImp implements TrolleyService {

	@Resource
	private TrolleyMapper trolleyMapper;   //注入台车mapper
	
	/**
	 * 获取所有的台车数据
	 * @return
	 */
	@Override
	public List<Trolley> findAllTrolley() {
		return trolleyMapper.findAllTrolley();
	}

	/**
	 * 分页查询台车数据
	 * @param map
	 * @return
	 */
	@Override
	public List<Trolley> listPageTrolley(Map<String, Object> map) {
		return trolleyMapper.listPageTrolley(map);
	}


	/**
	 * 获取总行数
	 * @param trolley
	 * @return
	 */
	@Override
	public Integer countTrolley(Trolley trolley) {
		return trolleyMapper.countTrolley(trolley);
	}

	/**
	 * 保存台车数据
	 * @param trolley
	 * @return
	 */
	@Override
	public Integer saveTrolley(Trolley trolley) {
		int isSuccess = -2;
		trolley.setRemark1("0");
		List<Trolley> list = trolleyMapper.getTrolleyByNumber(trolley.getTrocode());
		if(list.size() == 0){
			int i = trolleyMapper.saveTrolley(trolley);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改台车数据
	 * @param trolley
	 * @return
	 */
	@Override
	public Integer updateTrolley(Trolley trolley) {
		int isSuccess = -3;
		Trolley tro = trolleyMapper.getTrolleyById(trolley.getTid());
		if(tro.getTrocode().equals(trolley.getTrocode())){
			int i = trolleyMapper.updateTrolley(trolley);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			List<Trolley> list = trolleyMapper.getTrolleyByNumber(trolley.getTrocode());
			if(list.size() == 0){
				int i = trolleyMapper.updateTrolley(trolley);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 删除台车数据
	 * @param tids  台车ID组成的字符串
	 * @return
	 */
	@Transactional(rollbackFor = { Exception.class })
	public Integer deleteTrolley(String tids) {
		int isSuccess = 0;
		String[] split = tids.split(",");
		try {
			for (String tid : split) {
				isSuccess = trolleyMapper.deleteTrolley(Long.parseLong(tid));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

}
